package demo5;

public class Test5 {
    public static void main(String[] args) {
        System.out.println(findTheLongestBalancedSubstring("001"));
    }
    public static int findTheLongestBalancedSubstring(String s) {
        int len = s.length() ;
        int max = 0 ;
        for(int i = 0 ; i < len ; ){
            int a1 = 0 ;
            int b1 = 0 ;
            if(s.charAt(i) == '0'){
                while(i< len &&s.charAt(i) == '0'){
                    a1 ++ ;
                    i++ ;
                }
                while(i< len && s.charAt(i) == '1'){
                    b1++ ;
                    i++ ;
                    if(a1 == b1){
                        break ;
                    }
                }
                if(a1 == b1){
                    max = max < (a1+b1)?(a1+b1):max ;
                }
            }else{
                i ++ ;
            }
        }
        return max ;
    }}
